package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MyMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import org.springframework.web.bind.annotation.RequestBody;



@RestController
@RequestMapping("/permission")
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    /**
     * 获得所有权限
     * @return
     */
    @RequestMapping("/getAllPermission")
    public Result getAllPermission() {
        try {
            List<Permission> list = permissionService.getAllPermission();
            return new Result(true, MyMessageConstant.PERMISSION_QUERY_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MyMessageConstant.PERMISSION_QUERY_FAIL);
        }
    }

    //权限的分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean) throws Exception {

        //菜单分页查询的调用
        PageResult page = permissionService.findPage(pageBean);
        return page;
    }


    //权限添加
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission){

        try {
            //权限添加的调用
            permissionService.add(permission);
            return new Result(true, MessageConstant.ADD_P_SUCCESS);
        }
        catch (Exception e) {
            e.printStackTrace();
            //添加失败，给出提示
            return new Result(false,MessageConstant.ADD_P_PAIL);
        }
    }


    //编辑权限时，数据回显
    @RequestMapping("/findById")
    public Result findById(Integer id){

        try {

            //编辑权限时数据回显的调用
            Permission permission=permissionService.findById(id);
            return new Result(true,MessageConstant.GET_P_SUCCESS,permission);
        }
        catch (Exception e) {
            e.printStackTrace();
            //回显失败，给出提示
            return new Result(false,MessageConstant.GET_P_FAIL);

        }
    }

    //权限编辑修改
    @RequestMapping("/edit")
    public Result edit(@RequestBody Permission permission){

        try {
            //权限编辑的调用
            permissionService.edit(permission);

            return new Result(true,MessageConstant.EDIT_P_SUCCESS);
        }
        catch (Exception e) {
            e.printStackTrace();
            //编辑失败，给出提示
            return new Result(false,MessageConstant.EDIT_P_FAIL);
        }
    }

    //删除权限
    @RequestMapping("delete")
    public Result delete(Integer id){

        try {
            permissionService.delete(id);
            return new Result(true,MessageConstant.DELETE_P_SUCCESS);
        }
        catch (Exception e) {
            e.printStackTrace();
            //删除失败，给出提示
            return new Result(false,MessageConstant.DELETE_P_FAIL);
        }
    }

}
